using System;
using System.ComponentModel;
using System.Drawing;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Color=Microsoft.Xna.Framework.Graphics.Color;

namespace RebelStar.Engine.Graphics.MDX
{
    public class XNAGraphicsDevice : IGameGraphicsDevice
    {
        readonly Game game;

        public XNAGraphicsDevice(Game game)
        {
            this.game = game;
            game.Window.ClientSizeChanged += Window_ClientSizeChanged;
        }

        #region IGameGraphicsDevice Members
        public IServiceProvider ServiceProvider
        {
            get { return game.Services; }
        }
        public GraphicDeviceMode Mode
        {
            get { return GraphicDeviceMode.DirectX; }
        }
        public Size ClientSize
        {
            get { return new Size(game.Window.ClientBounds.Width, game.Window.ClientBounds.Height); }
        }

        public void Clear(System.Drawing.Color color)
        {
            Color col = new Color(color.R, color.G, color.B, color.A);

            game.GraphicsDevice.Clear(col);
        }
        public void Redraw()
        {
            if (Paint != null)
                Paint(this, EventArgs.Empty);
            game.GraphicsDevice.Present();
        }
        public void Present()
        {
            game.GraphicsDevice.Present();
        }

        public ISurface CreateSurface()
        {
            return new MDXSurface(new SpriteBatch(game.GraphicsDevice));
        }

        public ISurface CreateBuffer()
        {
            return new MDXSurface(new SpriteBatch(game.GraphicsDevice));
        }

        public event EventHandler Paint;
        public event EventHandler ClientSizeChanged;
        #endregion

        void Window_ClientSizeChanged(object sender, EventArgs e)
        {
            if (ClientSizeChanged != null) ClientSizeChanged(this, e);
        }
    }
}
